var api = require('../../utils/api.js');
var app = getApp();


Page({

    /**
     * 页面的初始数据
     */
    data: {
        tops: 32,
        users:[],
        images:[],
        img:'https://oa.stp1.cn:84/upload/20230310/b6ba40ed9826bd16ab84289f55d40c36.jpeg',
        hf:0,
        info:[],
        lists:[]
    },
        /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        var that=this;

        console.log(infos);
        var infos = wx.getSystemInfoSync();
        console.log(infos.statusBarHeight);

        this.setData({
            tops: infos.statusBarHeight + 4
        })

        this.setData({
            id:options.id
        })
   
        this.getinfobyid();
        this.getbackbyid();

    },
    //回复
    getbackbyid(){
        var that=this;

        that.setData({
            lists:[]
        })

        var id=this.data.id;
            //项目详情
            api.post({
            url: 'icons/getbackbyid',
            data: {id:id},
            success: data => {
                that.setData({
                    lists:data.data
                })
            }
        })
    },

    //详情
    getinfobyid(){
        var that=this;
        var id=this.data.id;
            //项目详情
            api.post({
            url: 'icons/getinfobyid',
            data: {id:id},
            success: data => {
                that.setData({
                    info:data.data
                })
            }
        })
    },


    previewImage(e) {

        var img=e.currentTarget.dataset.img;
        
       
        const searchField = "mp4";
        const containsField = img.includes(searchField);

        if(containsField){
            wx.previewMedia({
                sources: [{'url':img,'type':'video'}], // 需要预览的资源列表
            })
        }else{
            wx.previewMedia({
                sources: [{'url':img,'type':'image'}], // 需要预览的资源列表
            })
        }

    },


    seeimgs(e){
        console.log(e);
        var img=e.currentTarget.dataset.img;
        
       
        const searchField = "mp4";
        const containsField = img.includes(searchField);

        if(containsField){
            wx.previewMedia({
                sources: [{'url':img,'type':'video'}], // 需要预览的资源列表
            })
        }else{
            wx.previewMedia({
                sources: [{'url':img,'type':'image'}], // 需要预览的资源列表
            })
        }


    },

    closewind(){
        this.setData({
            hf:0
        })
    },
    windshow(){
        this.setData({
            hf:1
        })
    },



    goindex() {
        wx.navigateTo({
          url: '/pages/note/index',
        });
    },

    //上传信息
    uploadpdf(){

        let that = this;
        var usrl=api.HOST;
        var images=this.data.images;

        wx.chooseMedia({
            count: 9,
            mediaType: ['image'],
            sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success(res) { 
                console.log(res.tempFiles);
                let tempFilePaths = res.tempFiles;
                tempFilePaths.forEach(function(item){
                    wx.uploadFile({
                        filePath: item.tempFilePath,
                        name: 'file',
                        url: usrl+'/api/common/upload',
                        formData:{},
                        success:function(res){
                          var data= JSON.parse(res.data);
      
                          console.log(data);
                          
                          images.push(data.data.img)
                          that.setData({
                            images:images
                        })
                  
                        },
                        fail: function (res) {
                          console.log('上传失败');
                          }
                      })
                })
            }
        })
    },
    deleteimg(ret){
        console.log(ret);
        var i=ret.currentTarget.dataset.i;

        var images=this.data.images;
        var imgs=[];

        images.forEach(function(item,index){
            console.log(index);
            if(index!=i){
                imgs.push(item)
            }
        })

        console.log(imgs);

        this.setData({
            images:imgs
        })

    },

    usermsg(ret){
        this.setData({
            usermsg:ret.detail.value
        })
    },

    submits(){
        var usermsg=this.data.usermsg;
        var image=this.data.images;
        var id=this.data.id;
        var that=this;

            //项目详情
            api.post({
            url: 'icons/submitsbackmsg',
            data: {image:image,usermsg:usermsg,id:id},
            success: data => {
                console.log(data);

                wx.showToast({
                  title: data.msg,
                  icon:'none'
                })


                if(data.code == '1'){
                    that.setData({
                        hf:0
                    })

                    that.getbackbyid();
                }

            }
        })




    }

})